package com.neuedu.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *  *.jsp:过滤的是jsp页面
 *  
 * @author Administrator
 *
 */

public class CharacterFilter implements Filter{
   private FilterConfig config;
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		System.out.println("过滤的开始");
		this.config=filterConfig;
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req=(HttpServletRequest) request;
		HttpServletResponse resp=(HttpServletResponse)response;
		
		//提取
		ServletContext context=this.config.getServletContext();
		                  
		String charset=context.getInitParameter("charset");
		
		
		
		
		//post,get 传值
		req.setCharacterEncoding(charset);
		
		//响应
		response.setContentType("text/html;charset="+charset);
		response.setCharacterEncoding(charset);
		
		
		/*System.out.println("过滤前");*/
		chain.doFilter(request, response);//放行--这是一个链，当只有一个节点（过滤器）的时候，放行，当有多个节点的时候，执行下一个过滤器
		/*System.out.println("过滤后");*/
	}

	@Override
	public void destroy() {
		
		System.out.println("销毁");
	}

}
